<template>
  <el-dialog v-model="showAddForm" title="新建WG" width="30%">
    <el-form :model="appObject.form" label-width="120px" style="margin-top: 20px;" ref="formRef" :rules="rules">
      <el-form-item label="WG名字" prop="name">
        <el-input v-model="appObject.form.name"/>
      </el-form-item>
      <el-form-item label="WG描述" prop="description">
        <el-input v-model="appObject.form.description" type="textarea" />
      </el-form-item>
      <el-form-item label="WG日程" prop="schedule">
        <el-input v-model="appObject.form.schedule"/>
      </el-form-item>
      <el-form-item label="WG目录" prop="dir">
        <el-input v-model="appObject.form.dir"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="showAddForm = false">取消</el-button>
        <el-button type="primary" @click="submitForm(formRef)">确认</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
  import { reactive, ref } from 'vue'
  import { ElMessage } from 'element-plus'
  
  import {storeToRefs} from 'pinia'
  import wgStore from '../store/wg.js'
  const wStore = wgStore()
  const { showAddForm  } = storeToRefs(wStore)
  
  const appObject = reactive({
    form: {
      name: '',
      description: '',
      schedule: '',
      dir: ''
    }
  })
  
  // 表单校验
  const rules = reactive({
    name: [
      { required: true, message: '请输入WG名称', trigger: 'blur' },
    ],
    description: [
      { required: true, message: '请输入WG描述', trigger: 'blur' },
    ],
    schedule: [
      { required: true, message: '请输入WG日程', trigger: 'blur' },
    ],
    dir: [
      { required: true, message: '请输入WG目录', trigger: 'blur' },
    ]
  })
  
  const formRef = ref()
  
  const submitForm = (formEL) => {
    if (!formEL) return
    formEL.validate((valid) => {
      if (valid) {
        ElMessage({
          message: "WG创建成功",
          type: "success"
        })
        wStore.updateShowAddForm(false)
      }else{
        return false
      }
    })
  }
  
</script>

<style scoped>
</style>
